package com.atguigu.gulimall.product.controller;

import com.atguigu.common.to.SpuBoundTo;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.feign.CouponFeignService;
import com.atguigu.gulimall.product.feign.MemberFeignService;
import com.atguigu.gulimall.product.feign.OrderFeignService;
import com.atguigu.gulimall.product.service.CategoryService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.seata.core.context.RootContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * 商品三级分类
 *
 * @author ly
 */
@Slf4j
@RestController
@RequestMapping("product/category")
@AllArgsConstructor(onConstructor_ = @Autowired)
public class CategoryController {
    private static final Logger LOGGER = LoggerFactory.getLogger(CategoryController.class);


    private final CategoryService categoryService;

    /**
     * 查出所有分类以及子分类，以树形结构组装起来
     */
    @RequestMapping("/list/tree")
    public R list() {
        LOGGER.info("/list/tree");
        log.info("/////////");
        List<CategoryEntity> entities = categoryService.listWithTree();
        return R.ok().put("data", entities);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    public R info(@PathVariable("catId") Long catId) {
        CategoryEntity category = categoryService.getById(catId);
        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryEntity category) {
        categoryService.save(category);
        return R.ok();
    }

    /**
     * 菜单排序后修改信息
     */
    @RequestMapping("/update/sort")
    public R updateSort(@RequestBody CategoryEntity[] category) {
        categoryService.updateBatchById(Arrays.asList(category));
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryEntity category) {
        categoryService.updateCascade(category);
        return R.ok();
    }

    /**
     * 批量删除
     * RequestBody：获取请求体，必须发送POST请求
     * SpringMVC自动将请求体的数据（json），转为对应的对象
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] catIds) {
        categoryService.removeMenuByIds(Arrays.asList(catIds));
        return R.ok();
    }

    @Autowired
    private MemberFeignService memberFeignService;

    @Autowired
    private OrderFeignService orderFeignService;

//    @GlobalTransactional(timeoutMills = 300000, name = "spring-cloud-demo-tx")
    @GetMapping("/test/trans")
    public String trans() {
        log.info("Seata全局事务id=================>{}", RootContext.getXID());
//        CategoryEntity categoryEntity = new CategoryEntity();
//        categoryEntity.setName("name");
//        categoryEntity.setCatId(1L);
//        categoryService.save(categoryEntity);
        memberFeignService.saveMember();

//        Random random = new Random();
//        if (random.nextBoolean()) {
//           throw new RuntimeException("只保存了1");
//        }
//        orderFeignService.saveOrder();
        return "保存了2";
    }

}
